#include "sms.h"

/*
 *IconTextView * icontext_view_new(GooCanvasItem *parent, gdouble x, gdouble y, Theme *theme)
 *
 *user must Appoint the pic of bg, fg and icon
 *the width and height of the bg pic is the width and height of cavans group
 * */
IconTextView *icontext_view_new(GooCanvasItem *parent, gdouble x, gdouble y,
		                                               GdkPixbuf *pixbuf_bg, GdkPixbuf *pixbuf_fg, GdkPixbuf *pixbuf_icon,
		                                               gdouble icon_x, gdouble icon_y,
		                                               gdouble text_x, gdouble text_y, gdouble text_w, gchar *text,
		                                               Theme *theme) {
	IconTextView *itv;
	itv = (IconTextView *) g_malloc0(sizeof(IconTextView) + 1);
	itv->parent=parent;
	itv->x=x;
	itv->y=y;
	
	itv->summary_file = NULL;
	itv->next = NULL;	
	itv->privent = NULL;
	itv->theme = theme;
	
	if (text)
		itv->textbuf = text;
	else
		itv->textbuf = "";
	
	itv->w = gdk_pixbuf_get_width(pixbuf_bg);
	itv->h = gdk_pixbuf_get_height(pixbuf_bg);
	itv->pixbuf_bg = pixbuf_bg;
	itv->pixbuf_fg = pixbuf_fg;
	itv->cavas = goo_canvas_group_new(parent, "x", itv->x,
	                                          "y", itv->y,
	                                          "width", itv->w,
	                                          "height", itv->h, NULL);;
	itv->background = goo_canvas_image_new (itv->cavas, itv->pixbuf_bg,
                                                        0, 0, "scale-to-fit", TRUE, NULL);

	itv->text = goo_canvas_text_new(itv->cavas, itv->textbuf, text_x, text_y,
			                                        text_w, GTK_ANCHOR_WEST,
			                                        "font", theme->font[THEME_FONT_SMALL],
			                                        "ellipsize", PANGO_ELLIPSIZE_END,
			                                        "alignment", PANGO_ALIGN_CENTER, NULL);
	itv->icon = goo_canvas_image_new (itv->cavas, pixbuf_icon, icon_x, icon_y,
	 	                                          "scale-to-fit", TRUE, NULL);
	return itv;
}

/*
 * IconTextView *icontext_view_new_by_theme()
 *
 * use the Theme pic as the pic of the bg, fg and icon
 * */
IconTextView *icontext_view_new_by_theme(GooCanvasItem *parent, gdouble x, gdouble y,
		                                                        gdouble icon_x, gdouble icon_y,
		                                                        gdouble text_x, gdouble text_y, gdouble text_w, gchar *text,
		                                                        Theme *theme) {
	IconTextView *itv;
	itv = (IconTextView *) g_malloc0(sizeof(IconTextView) + 1);
	itv->parent=parent;
	itv->x=x;
	itv->y=y;

	itv->summary_file = NULL;
	itv->next = NULL;
	itv->privent = NULL;
	itv->theme = theme;

	if (text)
		itv->textbuf = text;
	else
		itv->textbuf = "";

	itv->w = gdk_pixbuf_get_width(theme->pic[THEME_PIC_ICON_TEXT_VIEW_BACKGROUND]);
	itv->h = gdk_pixbuf_get_height(theme->pic[THEME_PIC_ICON_TEXT_VIEW_BACKGROUND]);
	itv->pixbuf_bg = theme->pic[THEME_PIC_ICON_TEXT_VIEW_BACKGROUND];
	itv->pixbuf_fg = theme->pic[THEME_PIC_ICON_TEXT_VIEW_FOREGROUND];

	itv->cavas = goo_canvas_group_new(parent, "x", itv->x,
			                                  "y", itv->y,
			                                  "width", itv->w,
			                                  "height", itv->h, NULL);
	itv->background = goo_canvas_image_new (itv->cavas, theme->pic[THEME_PIC_ICON_TEXT_VIEW_BACKGROUND],
			                                            0, 0, "scale-to-fit", TRUE, NULL);
	itv->text = goo_canvas_text_new(itv->cavas, itv->textbuf, text_x, text_y,
			                                        text_w, GTK_ANCHOR_WEST,
			                                        "font", theme->font[THEME_FONT_SMALL],
			                                        "ellipsize", PANGO_ELLIPSIZE_END,
			                                        "alignment", PANGO_ALIGN_CENTER, NULL);
	itv->icon = goo_canvas_image_new (itv->cavas, NULL, icon_x, icon_y,
	 	                                          "scale-to-fit", TRUE, NULL);
}

void icontext_view_destroy(IconTextView *itv) {
	if(itv->background)
		g_object_unref(itv->background);
	if(itv->text)
		g_object_unref(itv->text);
	if(itv->cavas)
		g_object_unref(itv->cavas);
	if(itv->icon)
		g_object_unref(itv->icon);
	if(itv->pixbuf_bg)
		g_object_unref(itv->pixbuf_bg);
	if(itv->pixbuf_fg)
		g_object_unref(itv->pixbuf_fg);
	if(itv->textbuf)
		g_object_unref(itv->textbuf);
	if(itv->summary_file)
		g_object_unref(itv->summary_file);

	g_free(itv);
}

//gboolean icontext_view_change_view_frame_pic(IconTextView *itv, GdkPixbuf *pixbuf)

/*
 *gboolean  icontext_view_set_view_background_pic(IconTextView *itv, GdkPixbuf *pixbuf)
 *
 *set the bg pic, meanwhile set the width and height, cavas base on the new pic
 * */
gboolean icontext_view_set_view_background_pic(IconTextView *itv, GdkPixbuf *pixbuf) {
	if (pixbuf == NULL || itv == NULL)
		return FALSE;
	if (itv->pixbuf_bg)
		g_object_unref (itv->pixbuf_bg);
	itv->pixbuf_bg = pixbuf;

	/*itv->w = gdk_pixbuf_get_width(pixbuf);
	itv->h = gdk_pixbuf_get_height(pixbuf);
	itv->cavas = goo_canvas_group_new(itv->parent, "x", itv->x,
					                               "y", itv->y,
					                               "width", itv->w,
					                               "height", itv->h, NULL);
	itv->background = goo_canvas_image_new (itv->cavas, pixbuf, 0, 0, "scale-to-fit", TRUE, NULL);*/
	return TRUE;
}

gboolean icontext_view_set_view_foreground_pic(IconTextView *itv, GdkPixbuf *pixbuf) {
	if (pixbuf == NULL || itv == NULL)
		return FALSE;
	if (itv->pixbuf_fg)
		g_object_unref (itv->pixbuf_fg);
	itv->pixbuf_fg = pixbuf;
	return TRUE;
}

gboolean icontext_view_set_icon_pic(IconTextView *itv, GdkPixbuf *pixbuf) {
	if (itv->cavas == NULL)
		return FALSE;
	if (itv->icon)
			g_object_unref (itv->icon);
	g_object_set(itv->icon, "pixbuf", pixbuf, NULL);
	return TRUE;
}

gboolean icontext_view_set_icon_position(IconTextView *itv, gdouble x, gdouble y) {
	if (itv->cavas == NULL)
		return FALSE;
	if (itv->icon) {
		g_object_unref (itv->icon);
		itv->icon = NULL;
	}
	itv->icon = goo_canvas_image_new (itv->cavas, NULL, x, y, "scale-to-fit", TRUE, NULL);
	return TRUE;
}

gboolean icontext_view_set_text(IconTextView *itv, gdouble x, gdouble y, gchar *text) {
	if (itv->cavas == NULL)
		return FALSE;
	if (itv->textbuf) {
		g_object_unref (itv->textbuf);
		itv->textbuf = NULL;
	}
	
	if (itv->text) {
		g_object_unref (itv->text);
		itv->text = NULL;
	}
	itv->textbuf = text;
	itv->text = goo_canvas_text_new(itv->cavas, text, x, y,
			                                    280, GTK_ANCHOR_WEST,
			                                    "font", itv->theme->font[THEME_FONT_SMALL],
			                                    "ellipsize", PANGO_ELLIPSIZE_END, NULL);
	return TRUE;
}

/*Theme *listview_change_theme(Theme *theme) {
	
}*/
gboolean icontext_view_set_focus(IconTextView *itv) {
	if (itv->cavas == NULL || itv->background == NULL)
		return FALSE;
		
	g_object_set(itv->background, "pixbuf", itv->pixbuf_fg);
	return TRUE;
}

gboolean icontext_view_no_focus(IconTextView *itv) {
	if (itv->cavas == NULL || itv->background == NULL)
		return FALSE;
			
	g_object_set(itv->background, "pixbuf", itv->pixbuf_bg);
	return TRUE;
}

gboolean icontext_view_is_focus(IconTextView *itv) {

}

gchar *icontext_view_get_summary(IconTextView *itv) {

}

gboolean icontext_view_show(IconTextView *itv) {
	if(itv->theme->color[THEME_COLOR_TEXT_ACTIVE])
		g_object_set(itv->text, "fill_color", itv->theme->color[THEME_COLOR_TEXT_ACTIVE],NULL);
	
	//g_object_set(itv->icon, "pixbuf", itv->, NULL);
}
